package com.bjb.vr.operation.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjb.vr.common.ao.ResContentAo;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.ResContentService;
import com.bjb.vr.common.vo.OptContentVo;
import com.bjb.vr.common.vo.ResDictionaryVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @description: 课程资源管理
 * @author: hujingbo
 * @create: 2022-08-23 10:09
 **/
@RestController
@RequestMapping("/api/operation/v1")
public class SysResourceController {

    @Resource
    private ResContentService resContentService;

    /**
     * （运营端）课程资源字典列表查询
     *
     * @return
     */
    @PostMapping("/queryDictList")
    public BaseResult queryDictList(@RequestBody ResContentAo resContentAo) {
        List<ResDictionaryVo> dictionaryVos = resContentService.queryDictList(resContentAo);
        return CommonResult.Ok().setResult(dictionaryVos);
    }

    /**
     * （运营端）课程资源详情
     *
     * @return
     */
    @PostMapping("/queryResourcesOne")
    public BaseResult queryResourcesOne(@RequestBody ResContentAo resContentAo) {
        OptContentVo optContentVo = resContentService.queryResourcesOne(resContentAo);
        return CommonResult.Ok().setResult(optContentVo);
    }

    /**
     * （运营端）课程资源列表查询
     *
     * @return
     */
    @PostMapping("/queryResourcesList")
    public BaseResult queryResourcesList(@RequestBody ResContentAo resContentAo) {
        IPage<OptContentVo> resourcesList = resContentService.queryResourcesList(resContentAo);
        return CommonResult.Ok().setResult(resourcesList);
    }

    /**
     * （运营端）新增课程资源
     *
     * @return
     */
    @PostMapping("/addResource")
    public BaseResult addResource(@RequestBody ResContentAo resContentAo) {
        boolean success = resContentService.addResource(resContentAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok();
    }

    /**
     * （运营端）更新课程资源
     *
     * @return
     */
    @PostMapping("/updateResource")
    public BaseResult updateResource(@RequestBody ResContentAo resContentAo) {
        boolean success = resContentService.updateResource(resContentAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * （运营端）课程资源文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadResourceFile")
    public BaseResult uploadResourceFile(@RequestParam("fileName") MultipartFile file) throws IOException {
        if (file.getSize() == 0) {
            return CommonResult.Fail("文件不能为空");
        }
        JSONObject result = resContentService.uploadResourceFile(file);
        return CommonResult.Ok("上传成功！").setResult(result);
    }
}
